import Vue from "vue";
import VueRouter from "vue-router";
import router from "./router";
import store from "./store";
router.beforeEach((to, from, next) => {
  // 如果有token 说明该用户已登陆
  if (store.state.loginUserInfi) {
    // 在已登陆的情况下访问登陆页会重定向到首页
    if (to.path == "/login" || to.path == "/codelogin") {
      // next({ path: "/gas" });
      if (store.state.userQx[0].permissionid == 1) {
        next({ path: "/water" });
      } else if (store.state.userQx[0].permissionid == 2) {
        next({ path: "/gas" });

      } else if (store.state.userQx[0].permissionid == 3) {
        next({ path: "/forest" });

      } else if (store.state.userQx[0].permissionid == 4) {
        next({ path: "/wellLid" });

      }
    } else {
      // next({ path: to.path || "/" });
      next();
    }
  } else {
    // 没有登陆则访问任何页面都重定向到登陆页
    if (to.path == "/login" || to.path == "/codelogin") {
      next();
    } else {
      next(`/login?redirect=${to.path}`);
    }
  }
});

